package com.shushan.network.listener;

import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

import com.android.net.bean.NetRetBean;
import com.android.net.listener.async.NetHandleListener;
import com.android.net.listener.common.CallbackCode;
import com.shushan.base.Constants;
import com.shushan.shushanbao.BabyApplication;

import org.json.JSONException;

/**
 * @ClassName: NetStrCommListener
 * @Desciption: 返回字String的网络请求Listener
 * @author: yichaohua
 * @date: 2018-05-07
 */
abstract public class NetStrCommListener extends NetHandleListener {


    @Override
    protected void onReceivedRet(NetRetBean netRetBean) throws JSONException {

        int reset = netRetBean.getReset();
        if(reset == 1){
            Intent intent = new Intent();
            intent.setAction(Constants.USER_CHANGE_RECEIVER_ACTION);
            LocalBroadcastManager.getInstance(BabyApplication.getInstance()).sendBroadcast(intent);
        }

        netRetBean.setServerObject(netRetBean.getServerData());
        handleResult(netRetBean);
    }

    @Override
    protected void onSuccess(CallbackCode successCode, NetRetBean netRetBean) {
        onSuccess((String) netRetBean.getServerObject());
    }

    /**
     * 运行在ui线程，返回单个实体
     *
     * @param data 当前data
     */
    abstract protected void onSuccess(String data);
}
